import Vue from 'vue'
import Router from 'vue-router'
// 登录欢迎
// import Login from '../components/Login.vue'
const Login = () => import(/*webpackChunkName: "login" */ '../components/Login.vue')
// 首页和欢迎页面
// import Home from '../components/Home.vue'
const Home = () => import(/*webpackChunkName: "home" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/*webpackChunkName: "home" */ '../components/Welcome.vue')
// 用户管理
// import Users from '../components/user/Users.vue'
const Users = () => import(/*webpackChunkName: "users" */ '../components/user/Users.vue')
// import Rights from '../components/user/Rights.vue'
const Rights = () => import(/*webpackChunkName: "user" */ '../components/user/Rights.vue')
// import Roles from '../components/user/Roles.vue'
const Roles = () => import(/*webpackChunkName: "user" */ '../components/user/Roles.vue')

// import Categories from '../components/goods/Categories.vue'
const Categories = () => import(/*webpackChunkName: "goods" */ '../components/goods/Categories.vue')
// import Params from '../components/goods/Params.vue'
const Params = () => import(/*webpackChunkName: "goods" */ '../components/goods/Params.vue')
// import Goods from '../components/goods/Goods.vue'
const Goods = () => import(/*webpackChunkName: "goods" */ '../components/goods/Goods.vue')
// import Addgoods from '../components/goods/Addgoods.vue'
const Addgoods = () => import(/*webpackChunkName: "goods" */ '../components/goods/Addgoods.vue')

// import Order from '../components/order/Order.vue'
const Order = () => import(/*webpackChunkName: "order" */ '../components/order/Order.vue')

// import Reports from '../components/reports/Reports.vue'
const Reports = () => import(/*webpackChunkName: "reports" */ '../components/reports/Reports.vue')

Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/rights', component: Rights },
                { path: '/roles', component: Roles },
                { path: '/categories', component: Categories },
                { path: '/params', component: Params },
                { path: '/goods', component: Goods },
                { path: '/goods/add', component: Addgoods },
                { path: '/orders', component: Order },
                { path: '/reports', component: Reports },
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login') return next();
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) return next('/login');
    next();
})

export default router;